﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TowerClimber
{
    class Platform
    {
        #region PlatformVariables
        public Vector2 position;
        public bool underJumpable, slippery;
        public bool touchingPlayer = false;
        public string platformTextureAsset;
        public Rectangle platBoundaries;
        public Texture2D platTexture;
        int[] platformtypes;
        int Value;
        string type;
        #endregion

        public enum PlatformType
        {
            CommonSmall = 0,
            CommonMedium = 1,
            CommonLarge = 2,
            SolidSmall,
            SolidMedium,
            SolidLarge,
            IceSmall,
            IceMedium,
            IceLarge
        }
        
        public void initPlatform(Random random, int level)
        {
            #region Level

            switch (level)
            {
                case 1:
                    {
                        platformtypes = new int[] { 0, 1, 2 };
                    }
                    break;
                case 2:
                    {
                        platformtypes = new int[] { 0, 1, 2, 3, 4, 5 };
                    }
                    break;
                case 3:
                    {
                        platformtypes = new int[] { 0, 1, 2, 3, 4, 5, 6 };
                    }
                    break;
                case 4:
                    {
                        platformtypes = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8};
                    }
                    break;
                case 5:
                    {
                        platformtypes = new int[] { 0, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8 };
                    }
                    break;
                case 6:
                    {
                        platformtypes = new int[] { 2, 5, 8 };
                    }
                    break;
                case 7:
                    {
                        platformtypes = new int[] { 3, 4, 5, 6, 7, 8 };
                    }
                    break;
                case 8:
                    {
                        platformtypes = new int[] { 6, 7, 8 };
                    }
                    break;
                default:
                    {
                        platformtypes = new int[] { 6 };
                    }
                    break;
            }

            #endregion

            #region Platform Types
            Value = platformtypes[random.Next(platformtypes.Length)];
            type = Enum.GetName(typeof(PlatformType), Value);
            switch (type)
            {
                    
                case "CommonSmall":
                    {
                        platformTextureAsset = "platform_small";
                        underJumpable = true;
                    }
                    break;
                case "CommonMedium":
                    {
                        platformTextureAsset = "platform_medium";
                        underJumpable = true;
                    }
                    break;
                case "CommonLarge":
                    {
                        platformTextureAsset = "platform_large";
                        underJumpable = true;
                    }
                    break;
                case "SolidSmall":
                    {
                        platformTextureAsset = "solid_small";
                        underJumpable = false;
                    }
                    break;
                case "SolidMedium":
                    {
                        platformTextureAsset = "solid_medium";
                        underJumpable = false;
                    }
                    break;
                case "SolidLarge":
                    {
                        platformTextureAsset = "solid_large";
                        underJumpable = false;
                    }
                    break;
                case "IceSmall":
                    {
                        platformTextureAsset = "ice_small";
                        underJumpable = false;
                        slippery = true;
                    }
                    break;
                case "IceMedium":
                    {
                        platformTextureAsset = "ice_medium";
                        underJumpable = false;
                        slippery = true;
                    }
                    break;
                case "IceLarge":
                    {
                        platformTextureAsset = "ice_large";
                        underJumpable = false;
                        slippery = true;
                    }
                    break;
            #endregion

            }
        }

        public void scrollPlatform(float scrollspeed)
        {
            position.Y += scrollspeed;
        }

        public void drawPlatform(SpriteBatch spriteBatch)
        {
            platBoundaries = new Rectangle((int)this.position.X, (int)this.position.Y, this.platTexture.Width, this.platTexture.Height);
            spriteBatch.Draw(this.platTexture, platBoundaries, Color.White);
        }

        public void testCollision(Player player, Vector2 playerLastPosition)
        {
            
            if (new Rectangle((int)player.position.X, player.playerBoundaries.Bottom, player.playerBoundaries.Width, 4).Intersects(new Rectangle(platBoundaries.Left, platBoundaries.Top, platBoundaries.Width, 4)))
            {
                
                    player.onGround = true;
                    this.touchingPlayer = true;
                
            }
            else { this.touchingPlayer = false; }

            if (this.underJumpable == false)
            {
                if (new Rectangle((int)player.position.X, player.playerBoundaries.Top, player.playerBoundaries.Width, 4).Intersects(new Rectangle(platBoundaries.Left, platBoundaries.Bottom, platBoundaries.Width, 4)))
                {
                    player.jumpingUp = false;
                    player.onGround = false;
                }
            }

        }
    }
}
